import { Component, OnInit } from '@angular/core';
import {LoggerService} from "../../services/logger.service";
import {DataService} from "../../services/data.service"
declare var $:any;

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
  private data:any[];
  constructor(private _logger:LoggerService,private _data:DataService) { }

  ngOnInit() {
  	this._logger.getrandom();
    this._data.getdata().subscribe((res)=>{
      this.data=res;
    });
    //this._data.jsonp({id:1,name:'star'}).subscribe(res=>console.log(res),error=>console.log(error));
  }
  delete(item){
    this._data.post(item).subscribe(res=>{
      this.data.splice(this.remove(item),1);
    })
  }
  add(id,name){
    this._data.post({id:id,name:name}).subscribe(res=>{
      this.data.push(res);
    });
  }
  remove(item){
    var index;
    for(var i=0;i<this.data.length;i++){
      if(item.id==this.data[i].id){
        index=i;
        break;
      }
    }
    return index;
  }
  file(elfile){
    var file=elfile.files;
    var fd=new FormData();
    for(var i=0;i<file.length;i++){
      fd.append('upfile',file[i]);
    }
    this._data.upload(fd).subscribe(res=>{
      console.log(res);
    })
    elfile.value=''
  }
  callclick(event){
    console.log(event);
  }
}
